<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\model;
use think\Db;
class Commons
{
    /**删除文档和文档下的附表内容
     * @param $id:文档的id
     * @return bool
     */
    static function deleteDocument($id,$modelid){
//        $resul = Db::connect();
        $model = Db::name('model')->where('id',$modelid)->find();
       $data = Db::name($model['surface'])->where('uid',$id)->find();
        $seul = Db::name('table_field')->where('modelid',$modelid)->select();
        $arr = [];
       foreach ($seul as $value){
           if($value['field_type']=='单文件'){
               if($data[$value['field_title']]){
                   Qinu::DeleFile($data[$value['field_title']]);
               }
           }elseif($value['field_type']=='单图片'){
               if($data[$value['field_title']]){
                   Qinu::DeleFile($data[$value['field_title']]);
               }
           }elseif($value['field_type']=='视频'){
               if($data[$value['field_title']]){
                   Qinu::DeleFile($data[$value['field_title']]);
               }
           }elseif($value['field_type']=='多文件'){
               if($data[$value['field_title']]&&count(json_decode($data[$value['field_title']],true))){
                    $fe = json_decode($data[$value['field_title']],true);
                    foreach ($fe as $val){
                        Qinu::DeleFile($val);
                    }
               }
           }elseif($value['field_type']=='多图片'){
               if($data[$value['field_title']]&&count(json_decode($data[$value['field_title']],true))){
                   $fe = json_decode($data[$value['field_title']],true);
                   foreach ($fe as $val){
                       Qinu::DeleFile($val);
                   }
               }
           }
       }
       if(Db::name('document')->where('id',$id)->delete()&&Db::name($model['surface'])->where('uid',$id)->delete()){
           return true;
       }else{
           return false;
       }
    }

    /**递归查询文档数据数据
     * @param $biao:要查询的表
     * @param $id:要查询数据的id
     * @param $strid:主键
     * @param $strpid:上级的键名
     * @return array：所有数据
     */
    public function getData($id,$biao='document',$strid='id',$strpid='pid',$tiaojian=null){
        if($tiaojian){
            $array = Db::view($biao,true)
                ->view('model','model_name','document.model=model.id','left')
                ->where($strpid,$id)
                ->where('title','like',"%$tiaojian%")
                ->order('document.id desc')
                ->select();
            foreach ($array as $value){
                $data = $this->getData($value[$strid],$biao,$strid,$strpid);
                $array = array_merge($array,$data);
            }
        }else{
            $array = Db::view($biao)
                ->view('model','model_name','document.model=model.id','left')
                ->where($strpid,$id)
                ->order('id desc')
                ->select();
            foreach ($array as $value){
                $data = $this->getData($value[$strid],$biao,$strid,$strpid);
                $array = array_merge($array,$data);
            }
        }

        return $array;
    }
    public function getDatas($id,$biao='document',$strid='id',$strpid='pid',$tiaojian=null){
        if($tiaojian){
            $array = Db::view($biao,true)
                ->view('model','model_name','document.model=model.id','left')
                ->where('model.id','in',[21,7])
                ->where($strpid,$id)
                ->where('title','like',"%$tiaojian%")
                ->order('document.id desc')
                ->select();
//            foreach ($array as $value){
//                $data = $this->getData($value[$strid],$biao,$strid,$strpid);
//                $array = array_merge($array,$data);
//            }
        }else{
            $array = Db::view($biao)
                ->view('model','model_name','document.model=model.id','left')
                ->where('model.id','in',[21,7])
                ->where($strpid,$id)
                ->order('id desc')
                ->select();
//            foreach ($array as $value){
//                $data = $this->getData($value[$strid],$biao,$strid,$strpid);
//                $array = array_merge($array,$data);
//            }
        }

        return $array;
    }
    /**加密方式
     * @param $str:加密的数字或字符串
     * @return string:加密后的字符串
     */
    static function getPassword($str){
        return md5(md5($str));
    }
}